package com.example.demo.javatest.jdk8;

import org.apache.logging.log4j.util.Strings;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

import java.net.URI;
import java.util.Arrays;
import java.util.Date;
import java.util.Optional;

/**
 * @author ljh
 * @create 2019/10/23
 * @describe
 */
public class JavaTest {
    static final DateTime DISTRIBUTION_TIME_SPLIT_TIME = new DateTime().withTime(15,0,0,0);
    public static  String aa="sss";
    public static void main(String[] args) {
       URI uri= URI.create("https://page.didiglobal.com/driver-page/register?location_country=MX&lang=es-MX&utm=google&campaign=217&channel=104&country=MX&source=google&campaign_id=10383947167&ad_group_id=108942489091&creative_id=450065392486&target_id=kwd-494257128&keyword=&matchtype=p&devicemodel=&adposition=#");
       Optional<String> ss=Arrays.stream(uri.getQuery().split("&")).filter(p->p.contains("keyword")).map(p->{
          String [] keys= p.split("=");
          if(keys.length==2){
              return keys[1];
          }else{
              return Strings.EMPTY;
          }
        }).findFirst();
        System.out.println(ss.get());
    }

    /**
     * 如果今天下午3点前进行下单，那么发货时间是明天，如果今天下午3点后进行下单，
     * 那么发货时间是后天，如果被确定的时间是周日，那么在此时间上再加1天为发货时间。
     */
    public static void fun1(){
        System.out.println(calculateDistributionTimeByOrderCreateTime(new Date()));
    }

    private static Date calculateDistributionTimeByOrderCreateTime(Date orderCreateTime){
        DateTime orderCreateDateTime = new DateTime(orderCreateTime);
        Date tomorrow = orderCreateDateTime.plusDays(1).toDate();
        Date theDayAfterTomorrow = orderCreateDateTime.plusDays(2).toDate();
        return orderCreateDateTime.isAfter(DISTRIBUTION_TIME_SPLIT_TIME) ? wrapDistributionTime(theDayAfterTomorrow) : wrapDistributionTime(tomorrow);
    }
    private static Date wrapDistributionTime(Date distributionTime){
        DateTime currentDistributionDateTime = new DateTime(distributionTime);
        DateTime plusOneDay = currentDistributionDateTime.plusDays(1);
        boolean isSunday = (DateTimeConstants.SUNDAY == currentDistributionDateTime.getDayOfWeek());
        return isSunday ? plusOneDay.toDate() : currentDistributionDateTime.toDate() ;
    }
}
